// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    acount: "",
    password: 0,
    // 判断是否已经登录
    hasLogin: false,
    // 给当前页面渲染用的当前用户
    curUser: {},
    // 模拟数据库
    userAcoAndPas: [
      {acount: "admin1", password: 123456, name: "肥肥", userIma: "/static/my/头像1.jpg"},
      {acount: "admin2", password: 123456, name: "丫丫", userIma: "/static/my/头像2.jpg"},
      {acount: "admin3", password: 123456, name: "青青", userIma: "/static/my/头像3.jpg"},
      {acount: "admin4", password: 123456, name: "略略", userIma: "/static/my/头像4.jpg"},
      {acount: "admin5", password: 123456, name: "啪啪", userIma: "/static/my/头像5.jpg"},
    ],
    inWhere: "",
  },

  acoInput: function(e) {
    this.setData({
      acount: e.detail.value
    })
  },

  pasInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  login: function(e) {
    let a = this.data.acount;
    let p = this.data.password;
    // 获取app方便一会将数据保存在全局对象
    let app = getApp();
    // 前端验证
    if (a.length > 11) {
      wx.showToast({
        title: '账号不符合规定哦~',
        icon: "none"
      })
    } else if (p.length < 6 || p.length > 11) {
      wx.showToast({
        title: '密码不符合规定哦~',
        icon: "none"
      })
    } else {
      // 后端验证
      for (let i of this.data.userAcoAndPas) {
        if (a == i.acount && p == i.password) {
          let that = this;
          wx.showToast({
            title: '登录成功',
            icon: "none",
            success() {
              wx.setStorage({
                data: i,
                key: 'currentUser',
              })
              that.setData({
                hasLogin: true,
                curUser: i
              })
            }
          })
        }
      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getStorage({
      key: 'currentUser',
      success(res) {
        if (res.data.acount) {
          that.setData({
            curUser: res.data,
            hasLogin: true
          })
        } else {
          return;
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.startLocationUpdateBackground({
      success: (res) => {
        console.log(res);
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})